using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
using WPFExcelReport.Helpers;
using WPFExcelReport.Controls;
namespace WPFExcelReport
{
    public partial class VADAUFM
    {
        public DataRow Convert2ADAUFM(DataRow dwInput, string ITXDBVERSION, bool IsConvert = false)
        {
            DataSet SResult = new DataSet();
            SResult = QueryADAUFM(dwInput, 0);
            DataRow dwReturn = null;
            if (SResult.Tables[0].Rows.Count > 0)
            {
                dwReturn = SResult.Tables[0].Rows[0];
            }
            else
            {
                dwReturn = SResult.Tables[0].NewRow();
            }
            switch (ITXDBVERSION)
            {
                case "YGR":
                    #region=========
         dwReturn["ADAMANDANT"] = " ".PadRight(2 ); 
         dwReturn["ADAKUNDNR"] = " ".PadRight(8 ); 
         dwReturn["ADAWERK"] = " ".PadRight(3 ); 
         dwReturn["ADAABT"] = " ".PadRight(3 ); 
         dwReturn["ADAWAKZ"] = " ".PadRight(1 ); 
         dwReturn["ADAART"] = " ".PadRight(20 ); 
         dwReturn["ADAVAR"] = " ".PadRight(3 ); 
         dwReturn["ADAFARBE"] = " ".PadRight(10 ); 
         dwReturn["ADADESS"] = " ".PadRight(10 ); 
         dwReturn["ADACOL"] = " ".PadRight(10 ); 
         dwReturn["ADABESCH"] = " ".PadRight(10 ); 
         dwReturn["ADAZUSTAND"] = " ".PadRight(4 ); 
         dwReturn["ADALO"] = " ".PadRight(2 ); 
         dwReturn["ADAAUFM"] = " ".PadRight(2 ); 
         dwReturn["ADAAENZL"] = " ".PadRight(0 ); 
         dwReturn["ADAAENB"] = " ".PadRight(8 ); 
         dwReturn["ADAAEND"] = " ".PadRight(0 ); 
         dwReturn["ADANEUB"] = " ".PadRight(8 ); 
         dwReturn["ADANEUD"] = " ".PadRight(0 ); 
         dwReturn["ADAMINBST"] = " ".PadRight(0 ); 
         dwReturn["ADAMAXBST"] = " ".PadRight(0 ); 
         dwReturn["ADALOSGRMI"] = " ".PadRight(0 ); 
         dwReturn["ADALOSGRMA"] = " ".PadRight(0 ); 
         dwReturn["ADALOSGRFE"] = " ".PadRight(0 ); 
         dwReturn["ADAMINRW"] = " ".PadRight(0 ); 
         dwReturn["ADAMAXRW"] = " ".PadRight(0 ); 
         dwReturn["ADABASRW"] = " ".PadRight(0 ); 
         dwReturn["ADAPOSTYP"] = " ".PadRight(3 ); 
         dwReturn["ADAFILLER"] = " ".PadRight(57 ); 
                    #endregion
                    break;
                case "JLT":

                    break;
                default:

                    break;
            }
            if (IsConvert)
            {
                foreach (DataColumn cm in dwInput.Table.Columns)
                {
                    if (dwInput[cm.ColumnName].ToString() == "")
                        continue;
                    if (dwReturn.Table.Columns.Contains(cm.ColumnName))
                    {
                        dwReturn[cm.ColumnName] = dwInput[cm.ColumnName];
                    }
                }
            }
            return dwReturn;
        }
        public void MapADAUFM(ref DataRow dwLocal, DataRow dwData)
        {
            BlindMap(ref dwLocal, dwData);
        }
        public void SaveADAUFM(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            DataSet setResult = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;
            setResult = QueryADAUFM(dwData);
            if (setResult.Tables.Count > 0)
            {
                tbLocal = setResult.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }
            MapADAUFM(ref dwLocal, dwData);

            if (!flgExist)
            {

                InsertADAUFM(dwLocal, ITXDBVERSION);
            }
            UpdateADAUFM(dwLocal);
        }
        public void InsertADAUFM(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            switch (ITXDBVERSION)
            {
                case "YGR":
                    or1.CommandText = "INSERT INTO ADAUFM(VAKBMANDANT, VAKBKOLL, VAKBART, VAKBKUNDNR, VAKBFARBE, VAKBKDARTNR, VAKBKDARTBEZ, VAKBKDFARBNR, VAKBANDRUCK, VAKBMEGEW, VAKBMENGEH, VAKBGEWVON, VAKBGEWBIS, VAKBGEWCA, VAKBGEWFKT, VAKBGEWGRP, VAKBMEGFKT, VAKBMEGGRP, VAKBAENDZL, VAKBAENDBEN, VAKBAENDDAT, VAKBNEUBEN, VAKBNEUDAT, VAKBFILLER)"
                        + " VALUES (:VAKBMANDANT, :VAKBKOLL, :VAKBART, :VAKBKUNDNR, :VAKBFARBE, '                         ', '                         ', '                             ', ' ', '   ', '   ', 0, 0, ' ', '000000000000', ' ', '000000000000', ' ', 0, '        ', '000000', '        ', '      ', '                                          ')";
                    or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                //VAFMANDANT VAFKOLL VAFART VAFFARBE VAFBEZ VAFFFARBE

            }
            
            Connect2Proxy(or1);
        }
        public DataSet QueryADAUFM(DataRow dwData, int option = 1)
        {
            DataSet Return;
            DataSet Result = new DataSet();
            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 0:
                    or1.CommandText = "SELECT * FROM ADAUFM WHERE  ROWNUM<2 ";
                    break;
              
                case 99:
                    or1.CommandText = "SELECT * FROM ADAUFM  WHERE ROWNUM<50";
                    break;
                default:
                    break;
            }
            Result = Connect2Proxy(or1);
            if (Result.Tables.Count > 0)
                Result.Tables[0].TableName = "ADAUFM";
            Return = Result;
            return Return;
        }
        public void DeleteADAUFM(DataRow dwData, int option = 1)
        {

            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "DELETE FROM ADAUFM   WHERE  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                     or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                default:
                    break;
            }
            Connect2Proxy(or1);
        } 
      
        public void UpdateADAUFM(DataRow dwData, string ITXDBVERSION = "50.24", int option = 1)
        {
            OracleCommand or1;
            DataSet Result = new DataSet();
            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    #region====Command==== 
                    or1.CommandText = "UPDATE ADAUFM SET "
                    +"ADAMANDANT= :ADAMANDANT,"
                    +"ADAKUNDNR= :ADAKUNDNR,"
                    +"ADAWERK= :ADAWERK,"
                    +"ADAABT= :ADAABT,"
                    +"ADAWAKZ= :ADAWAKZ,"
                    +"ADAART= :ADAART,"
                    +"ADAVAR= :ADAVAR,"
                    +"ADAFARBE= :ADAFARBE,"
                    +"ADADESS= :ADADESS,"
                    +"ADACOL= :ADACOL,"
                    +"ADABESCH= :ADABESCH,"
                    +"ADAZUSTAND= :ADAZUSTAND,"
                    +"ADALO= :ADALO,"
                    +"ADAAUFM= :ADAAUFM,"
                    +"ADAAENZL= :ADAAENZL,"
                    +"ADAAENB= :ADAAENB,"
                    +"ADAAEND= :ADAAEND,"
                    +"ADANEUB= :ADANEUB,"
                    +"ADANEUD= :ADANEUD,"
                    +"ADAMINBST= :ADAMINBST,"
                    +"ADAMAXBST= :ADAMAXBST,"
                    +"ADALOSGRMI= :ADALOSGRMI,"
                    +"ADALOSGRMA= :ADALOSGRMA,"
                    +"ADALOSGRFE= :ADALOSGRFE,"
                    +"ADAMINRW= :ADAMINRW,"
                    +"ADAMAXRW= :ADAMAXRW,"
                    +"ADABASRW= :ADABASRW,"
                    +"ADAPOSTYP= :ADAPOSTYP,"
                    +"ADAFILLER= :ADAFILLER,"
                        + " WHERE  "
                        + "  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                    #endregion

                    #region====Parameters====
          or1.Parameters.Add("ADAMANDANT", OracleType.NVarChar).Value = dwData["ADAMANDANT"].ToString(); 
          or1.Parameters.Add("ADAKUNDNR", OracleType.NVarChar).Value = dwData["ADAKUNDNR"].ToString(); 
          or1.Parameters.Add("ADAWERK", OracleType.NVarChar).Value = dwData["ADAWERK"].ToString(); 
          or1.Parameters.Add("ADAABT", OracleType.NVarChar).Value = dwData["ADAABT"].ToString(); 
          or1.Parameters.Add("ADAWAKZ", OracleType.NVarChar).Value = dwData["ADAWAKZ"].ToString(); 
          or1.Parameters.Add("ADAART", OracleType.NVarChar).Value = dwData["ADAART"].ToString(); 
          or1.Parameters.Add("ADAVAR", OracleType.NVarChar).Value = dwData["ADAVAR"].ToString(); 
          or1.Parameters.Add("ADAFARBE", OracleType.NVarChar).Value = dwData["ADAFARBE"].ToString(); 
          or1.Parameters.Add("ADADESS", OracleType.NVarChar).Value = dwData["ADADESS"].ToString(); 
          or1.Parameters.Add("ADACOL", OracleType.NVarChar).Value = dwData["ADACOL"].ToString(); 
          or1.Parameters.Add("ADABESCH", OracleType.NVarChar).Value = dwData["ADABESCH"].ToString(); 
          or1.Parameters.Add("ADAZUSTAND", OracleType.NVarChar).Value = dwData["ADAZUSTAND"].ToString(); 
          or1.Parameters.Add("ADALO", OracleType.NVarChar).Value = dwData["ADALO"].ToString(); 
          or1.Parameters.Add("ADAAUFM", OracleType.NVarChar).Value = dwData["ADAAUFM"].ToString(); 
          or1.Parameters.Add("ADAAENZL", OracleType.NVarChar).Value = dwData["ADAAENZL"].ToString(); 
          or1.Parameters.Add("ADAAENB", OracleType.NVarChar).Value = dwData["ADAAENB"].ToString(); 
          or1.Parameters.Add("ADAAEND", OracleType.NVarChar).Value = dwData["ADAAEND"].ToString(); 
          or1.Parameters.Add("ADANEUB", OracleType.NVarChar).Value = dwData["ADANEUB"].ToString(); 
          or1.Parameters.Add("ADANEUD", OracleType.NVarChar).Value = dwData["ADANEUD"].ToString(); 
          or1.Parameters.Add("ADAMINBST", OracleType.NVarChar).Value = dwData["ADAMINBST"].ToString(); 
          or1.Parameters.Add("ADAMAXBST", OracleType.NVarChar).Value = dwData["ADAMAXBST"].ToString(); 
          or1.Parameters.Add("ADALOSGRMI", OracleType.NVarChar).Value = dwData["ADALOSGRMI"].ToString(); 
          or1.Parameters.Add("ADALOSGRMA", OracleType.NVarChar).Value = dwData["ADALOSGRMA"].ToString(); 
          or1.Parameters.Add("ADALOSGRFE", OracleType.NVarChar).Value = dwData["ADALOSGRFE"].ToString(); 
          or1.Parameters.Add("ADAMINRW", OracleType.NVarChar).Value = dwData["ADAMINRW"].ToString(); 
          or1.Parameters.Add("ADAMAXRW", OracleType.NVarChar).Value = dwData["ADAMAXRW"].ToString(); 
          or1.Parameters.Add("ADABASRW", OracleType.NVarChar).Value = dwData["ADABASRW"].ToString(); 
          or1.Parameters.Add("ADAPOSTYP", OracleType.NVarChar).Value = dwData["ADAPOSTYP"].ToString(); 
          or1.Parameters.Add("ADAFILLER", OracleType.NVarChar).Value = dwData["ADAFILLER"].ToString(); 
                    #endregion
                    break; 
                default:
                    break;
            }


            Connect2Proxy(or1);
        }
    }
}
